import { useSelector } from 'react-redux';
import { selectActiveTab } from '@/store/tabs';

const hasElementAuth = (permission) => {
  // 从Redux store中获取按钮权限数据
  const authElement = useSelector(state => state.user.authElement);
  // 从 Redux store 获取当前激活的标签页
  const activeTab = useSelector(selectActiveTab);
  if(null==authElement){
    return true;
  }
  const currentPageRoles = authElement[activeTab?.name || ''] ?? [];

  // 如果没有提供权限，则返回false
  if (!permission) {
    return false;
  }

  let hasPermission = false;
  if (permission instanceof Array) {
    // 对于空数组，我们返回true（表示不需要特定权限）
    if (permission.length === 0) {
      hasPermission = true;
    } else {
      // 只要用户拥有其中一个权限即可
      hasPermission = permission.some(item => currentPageRoles.includes(item));
    }
  } else {
    hasPermission = currentPageRoles.includes(permission);
  }
  return hasPermission;
}

export default hasElementAuth;